package visual_db

import (
	"cgs-server/server/base"
	"cgs-server/server/model"
	"go.mongodb.org/mongo-driver/bson"
)

type DbService interface {
	base.Service
	TestDb(db *Db, key []byte) error
}

func NewDbService() DbService {
	return &dbService{
		ServiceImpl: base.ServiceImpl{Repo: NewMapRepository()},
	}
}

type dbService struct {
	base.ServiceImpl
}

func (d *dbService) TestDb(db *Db, key []byte) error {
	return nil
}

func (d *dbService) List(mdl *model.DataUserModel) (interface{}, error) {
	if _, ok := mdl.Data.(*Db); ok {
		// 查找列表
		results, err := d.Repo.Find(mdl)
		if err != nil {
			return nil, err
		}
		// 计数
		mdl.Filter = &bson.M{"isDeleted": 0}
		count, err := d.Repo.Count(mdl)

		res := &model.PageResult{
			Records: results,
			Total:   count,
			Size:    mdl.Page.Size,
		}

		return res, err
	} else {
		return nil, model.NewTypeError("VisualMap")
	}
}
